﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using MaxStudio.Concrete.DBUtility;
using MaxStudio.Concrete.Server.Dao;
using MaxStudio.Concrete.Server.Interface; 

namespace MaxStudio.Concrete.Server.Service
{
    public class CustomerService : ServiceHandle, ICustomer
    {
        #region ICustomer Members

      //  ConcreteDaoDataContext dataContext = new ConcreteDaoDataContext();

        public OperationResult<List<tb_Customer>> GetAllCustomer(int currentpage, int pageSize)
        {
            ConcreteDaoDataContext dataContext = getDateContext();

            OperationResult<List<tb_Customer>> result = new OperationResult<List<tb_Customer>>();
            List<tb_Customer> list = new List<tb_Customer>();
            try
            {
                list = dataContext.GetAllCustomerList(currentpage, pageSize);

                if (list == null)
                {
                    result.errorMessageKey = "Error";
                }
                else
                {
                    result.obj = list;
                    result.operationSuccess = true;
                }
            }
            catch (Exception ex)
            {
                result.exceptionString = DlHelper.ExceptionString(ex);

            }
            return result;
        }

        public OperationResult<tb_Customer> GetCustomerById(int id)
        {
            ConcreteDaoDataContext dataContext = getDateContext();

            OperationResult<tb_Customer> result = new OperationResult<tb_Customer>();

            try
            {
                tb_Customer entity = new tb_Customer();
                entity = dataContext.GetCustomerById(id);

                if (entity == null)
                {
                    result.errorMessageKey = "Error";
                }
                else
                {
                    result.obj = entity;
                    result.operationSuccess = true;
                }
            }
            catch (Exception ex)
            {
                result.exceptionString = DlHelper.ExceptionString(ex);

            }
            return result;
        }

        public OperationResult<tb_Customer> CreateCustomer(tb_Customer entity)
        {
            ConcreteDaoDataContext dataContext = getDateContext();

            OperationResult<tb_Customer> result = new OperationResult<tb_Customer>();
            
            try
            {
                dataContext.CreateCustomer(entity);
                dataContext.SubmitChanges();
                result.obj = entity;
                result.operationSuccess = true;
            }
            catch (Exception ex)
            {
                result.exceptionString = DlHelper.ExceptionString(ex);
            }

            return result;
        }

        public OperationResult<tb_Customer> ModifyCustomer(tb_Customer entity)
        { 
            OperationResult<tb_Customer> result = new OperationResult<tb_Customer>();
            ConcreteDaoDataContext dataContext = getDateContext();
  
            try
            {
                dataContext.ModifyCustomer(entity);
                dataContext.SubmitChanges();
                result.obj = entity;
                result.operationSuccess = true;
            }
            catch (Exception ex)
            {
                result.exceptionString = DlHelper.ExceptionString(ex);
            }

            return result;
        }

        public OperationResult<tb_Customer> DeleteCustomer(tb_Customer entity)
        {
            OperationResult<tb_Customer> result = new OperationResult<tb_Customer>();
            ConcreteDaoDataContext dataContext = getDateContext();

            try
            {
                dataContext.DeleteCustomer(entity.Id);
                dataContext.SubmitChanges();
                result.obj = entity;
                result.operationSuccess = true;
            }
            catch (Exception ex)
            {
                result.exceptionString = DlHelper.ExceptionString(ex);
            }

            return result;
        }

        public OperationResult<List<tb_Customer>> GetCustomerByMasterType(int masterTypeId)
        { 

            OperationResult<List<tb_Customer>> result = new OperationResult<List<tb_Customer>>();
            //List<tb_Customer> list = new List<tb_Customer>();
            //try
            //{
            //    list = dataContext.GetCustomerById 

            //    if (list == null)
            //    {
            //        result.errorMessageKey = "Error";
            //    }
            //    else
            //    {
            //        result.obj = list;
            //        result.operationSuccess = true;
            //    }
            //}
            //catch (Exception ex)
            //{
            //    result.exceptionString = DlHelper.ExceptionString(ex);

            //}
            return result;
        }

        #endregion

       
    }
}
